<?php

namespace Admin\Controller;

use Think\Controller;

class AdminController extends Controller
{
    public function _initialize()
    {
        // parent::_initialize();
        // dump(CONTROLLER_NAME);
        $auth = new \Think\Auth();

        if (!session('user')) {
            $url = U('Public/index');
            echo "<script> top.location.href=" . $url . "; </script>";
            echo "<meta http-equiv='Refresh' content='0;URL=$url'>";
            exit;
        }
        //如果是超级管理员，就有所有的权限。
        //dump(session('user.part')['totaldata']);
        //exit;
        //$this->update_user_time();//删除这个

        $operation_obj = new \Org\Util\Operation();
        //记录日志
        //$operation_obj -> checkTableIsExist();//创建日志记录表，如果不存在则新建
        if (CONTROLLER_NAME != 'Index' && 'Portal' != CONTROLLER_NAME && 'Layout' != CONTROLLER_NAME && 'Log' != CONTROLLER_NAME) {
            if (IS_POST) {
                $postParm = json_encode($_POST, JSON_UNESCAPED_UNICODE);
                $operation_obj->writeLog(APP_PATH . '' . MODULE_NAME . '/' . CONTROLLER_NAME . '/' . ACTION_NAME . $postParm, '系统后台用户直接操作');
            }
        }

        if (session('user.account') == 'admin') {
            return true;
        }
        $authFlag = $auth->check(MODULE_NAME . '/' . CONTROLLER_NAME . '/' . ACTION_NAME, session('user.id'));
        if (!$authFlag) {
            $this->error("你没有权限！请联系管理员赋予您权限！", 0);
            exit;
        }
    }

    public function update_user_time()
    {
        $data['id'] = session('user.id');
        $data['update_time'] = date("Y-m-d H:i:s", time());
        M('User')->save($data);
    }
}
